<?php
namespace Wechat\Controller;

class AdminController extends \Common\Controller\BaseController{

	public function _initialize(){
		parent::_initialize();
		$this->wechatmenu = D('Wechatmenu');
		$top_nav = array(
				array('name'=>'微信菜单','url'=>U('Wechat/Admin/menu'),'tab'=>array('menu','add_menu','eidt_menu')),
				array('name'=>'添加菜单','url'=>U('Wechat/Admin/add_menu'),'right'=>1),
		);
		$this->assign('top_nav',$top_nav);
	}

	public function menu(){
		$list = $this->wechatmenu->field($field)->select();
		$list = list_to_tree($list);
		$this->assign('list',$list);
		$this->display();
	}

	public function add_menu(){
		if (IS_POST) {
			$data = $this->wechatmenu->create();
			if ($data) {
				if ($data['keyword'] == '' && $data['url'] == '') {
					$this->error("关键词或者URL必填一项！");
				}
				//判断类型
				if ($data['keyword'] == '') {
					$data['type'] = 'view';
				}else{
					$data['type'] = 'click';
				}
				$count = $this->wechatmenu->where(array('pid'=>$data['pid']))->count();
				if ($data['pid'] == 0 && $count >= 3) {
					$this->error('一级菜单不能多于三个！');
				}elseif($data['pid'] != 0 && $count >= 5){
					$this->error('二级菜单不能多于五个！');
				}

				$result = $this->wechatmenu->add($data);
				if ($result) {
					$this->success('添加成功！',U('Wechat/Admin/menu'));
				}else{
					$this->error("添加失败！");
				}
				
			}else{
				$this->error($this->wechatmenu->getError());
			}
		}else{
			$list = $this->wechatmenu->where(array('pid'=>0))->select();
			$this->assign('cate_list',$list);
			$this->display();
		}
	}

	public function eidt_menu(){
		if (IS_POST) {
			$data = $this->wechatmenu->create();
			if ($data) {
				if ($data['keyword'] == '' && $data['url'] == '') {
					$this->error("关键词或者URL必填一项！");
				}
				//判断类型
				if ($data['keyword'] == '') {
					$data['type'] = 'view';
				}else{
					$data['type'] = 'click';
				}

				$result = $this->wechatmenu->save($data);
				if ($result) {
					$this->success('修改成功！',U('Wechat/Admin/menu'));
				}else{
					$this->error("修改失败！");
				}
				
			}else{
				$this->error($this->wechatmenu->getError());
			}
		}else{
			$id = I('id','','intval');
			if ($id == '') {
				$this->error("非法操作！");
			}
			$data = $this->wechatmenu->where(array('id'=>$id))->find();
			$list = $this->wechatmenu->where(array('pid'=>0))->select();
			$this->assign('cate_list',$list);
			$this->assign($data);
			$this->display('Admin_add_menu');
		}
	}

	public function del_menu(){
		$id = I('id','','intval');

		if ($id == '') {
			$this->error("非法操作！");
		}

		$result = $this->wechatmenu->where(array(
			'id' => $id,
		))->delete();
		if ($result) {
			$this->success("删除成功！");
		}else{
			$this->error("删除失败！");
		}
	}

	public function creatmenu(){
		$wechat = new \Com\WeChat\WechatAuth($this->conf['appid'],$this->conf['appsecret']);
		$wechat->getAccessToken();

		$list = $this->wechatmenu->where(array(
			'pid' => 0,
		))->order('sort asc')->select();
		foreach ($list as $key => $value) {
			$rs = $this->wechatmenu->field('name,keyword as `key`,type,url')->where(array(
				'pid' => $value['id'],
			))->order('sort asc')->select();
			if (empty($rs)) {
				$data[] = array(
					'name'   => $value['name'],
					'type'   => $value['type'],
					'key'    => $value['keyword'],
					'url'    => $value['url'],
				);
			}else{
				$data[] = array(
					'name'   => $value['name'],
					'sub_button' => $rs
				);
			}
		}

		$result = $wechat->menuCreate($data);

		$this->success($result['errmsg']);
	}
}